package com.sgcc.lms.openstack.controller;

import com.sgcc.lms.openstack.exception.NullTokenException;
import com.sgcc.lms.openstack.model.po.Instance;
import com.sgcc.lms.openstack.service.NovaService;
import com.sgcc.lms.openstack.util.OpenStackClientFactory;
import com.sgcc.lms.openstack.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.openstack4j.api.OSClient;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.*;
import org.openstack4j.openstack.compute.domain.NovaFlavor;
import org.openstack4j.openstack.compute.domain.NovaKeypair;
import org.openstack4j.openstack.compute.domain.NovaQuotaSetUpdate;
import org.openstack4j.openstack.compute.domain.NovaServerCreate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-07-23 09:01
 * @Description: 计算服务控制层
 */
@RestController
@Slf4j
@Api(tags = "NovaController：计算服务")
public class NovaController {
    @Autowired
    private NovaService novaService;
    private static final String LINE = "==========================================》{}";

    /**
     * @Author 李孟帅
     * @Description 列出实例类型
     * @Date 2020/7/23
     **/
    @GetMapping("/flavors")
    @ApiOperation("列出实例类型")
    public ResultUtil<?> flavors(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Flavor> flavors = novaService.flavors(tokenId);
        log.info(uri + ",列出实例类型" + LINE, "成功");
        return ResultUtil.success(flavors);
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例类型
     * @Date 2020/7/23
     **/
    @GetMapping("/flavors/get")
    @ApiOperation("获取实例类型")
    public ResultUtil<?> getFlavor(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Flavor flavor = novaService.getFlavor(tokenId, id);
        log.info(uri + ",获取实例类型" + LINE, "成功");
        return ResultUtil.success(flavor);
    }

    /**
     * @Author 李孟帅
     * @Description 删除实例类型
     * @Date 2020/7/23
     **/
    @GetMapping("/flavors/delete")
    @ApiOperation("删除实例类型")
    public ResultUtil<?> deleteFlavor(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse response = novaService.deleteFlavor(tokenId, id);
        log.info(uri + ",删除实例类型" + LINE, "成功");
        return ResultUtil.success(response);
    }

    /**
     * @Author 李孟帅
     * @Description 创建实例类型
     * @Date 2020/7/23
     **/
    @PostMapping("/flavors/create")
    @ApiOperation("创建实例类型")
    public ResultUtil<?> createFlavor(HttpServletRequest request, @RequestBody NovaFlavor flavor) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Flavor flavor1 = novaService.createFlavor(tokenId, flavor);
        log.info(uri + ",创建实例类型" + LINE, "成功");
        return ResultUtil.success(flavor1);
    }

    /**
     * @Author 李孟帅
     * @Description 获取特定项目的配额集
     * @Date 2020/7/23
     **/
    @GetMapping("/quotaSets/get")
    @ApiOperation("获取特定项目的配额集")
    public ResultUtil<?> getQuotaSet(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String projectId = OpenStackClientFactory.getProjectId(tokenId);
        QuotaSet quotaSet = novaService.getQuotaSet(tokenId, projectId);
        log.info(uri + ",获取特定项目的配额集" + LINE, "成功");
        return ResultUtil.success(quotaSet);
    }

    /**
     * @Author 李孟帅
     * @Description 更新特定项目的配额集
     * @Date 2020/7/23
     **/
    @PostMapping("/quotaSets/update")
    @ApiOperation("更新特定项目的配额集")
    public ResultUtil<?> updateQuotaSet(HttpServletRequest request, @RequestBody NovaQuotaSetUpdate quotaSetUpdate) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String projectId = OpenStackClientFactory.getProjectId(tokenId);
        QuotaSet quotaSet = novaService.updateQuotaSet(tokenId, projectId, quotaSetUpdate);
        log.info(uri + ",更新特定项目的配额集" + LINE, "成功");
        return ResultUtil.success(quotaSet);
    }

//    /**
//     * @Author 李孟帅
//     * @Description 恢复项目的默认配额集
//     * @Date 2020/7/23
//     **/
//    @GetMapping("/quotaSets/delete")
//    public ResultUtil<?> deleteQuotaSet(HttpServletRequest request) throws NullTokenException {
//        return ResultUtil.success();
//    }

    /**
     * @Author 李孟帅
     * @Description 列出密钥对
     * @Date 2020/7/23
     **/
    @GetMapping("/keypairs")
    @ApiOperation("列出密钥对")
    public ResultUtil<?> keypairs(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Keypair> keypairs = novaService.keypairs(tokenId);
        log.info(uri + ",列出密钥对" + LINE, "成功");
        return ResultUtil.success(keypairs);
    }

    /**
     * @Author 李孟帅
     * @Description 获取密钥对
     * @Date 2020/7/23
     **/
    @GetMapping("/keypairs/get")
    @ApiOperation("获取密钥对")
    public ResultUtil<?> getKeypair(HttpServletRequest request, @RequestParam("name") String name) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Keypair keypair = novaService.getKeypair(tokenId, name);
        log.info(uri + ",获取密钥对" + LINE, "成功");
        return ResultUtil.success(keypair);
    }

    /**
     * @Author 李孟帅
     * @Description 删除密钥对
     * @Date 2020/7/23
     **/
    @GetMapping("/keypairs/delete")
    @ApiOperation("删除密钥对")
    public ResultUtil<?> deleteKeypair(HttpServletRequest request, @RequestParam("name") String name) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse response = novaService.deleteKeypair(tokenId, name);
        log.info(uri + ",删除密钥对" + LINE, "成功");
        return ResultUtil.success(response);
    }

    /**
     * @Author 李孟帅
     * @Description 创建密钥对
     * @Date 2020/7/23
     **/
    @PostMapping("/keypairs/create")
    @ApiOperation("创建密钥对")
    public ResultUtil<?> createKeypair(HttpServletRequest request, @RequestBody NovaKeypair keypair) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        String name = keypair.getName();
        String publicKey = keypair.getPublicKey();
        Keypair keypair1 = novaService.createKeypair(tokenId, name, publicKey);
        log.info(uri + ",创建密钥对" + LINE, "成功");
        return ResultUtil.success(keypair1);
    }

    /**
     * @Author 李孟帅
     * @Description 列出实例
     * @Date 2020/7/24
     **/
    @GetMapping("/servers")
    @ApiOperation("列出实例")
    public ResultUtil<?> servers(HttpServletRequest request) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        List<? extends Server> servers = novaService.servers(tokenId);
        log.info(uri + ",列出实例" + LINE, "成功");
        return ResultUtil.success(servers);
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例
     * @Date 2020/7/24
     **/
    @GetMapping("/servers/get")
    @ApiOperation("获取实例")
    public ResultUtil<?> getServer(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Server server = novaService.getServer(tokenId, id);
        log.info(uri + ",获取实例" + LINE, "成功");
        return ResultUtil.success(server);
    }


    /**
     * @Author 李孟帅
     * @Description 创建实例
     * @Date 2020/7/24
     **/
    @PostMapping("/servers/create")
    @ApiOperation("创建实例")
    public ResultUtil<?> createServer(HttpServletRequest request, @RequestBody Instance instance) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        Server server = novaService.createServer(tokenId, instance);
        log.info(uri + ",创建实例" + LINE, "成功");
        return ResultUtil.success(server);
    }

    /**
     * @Author 李孟帅
     * @Description 删除实例
     * @Date 2020/7/24
     **/
    @GetMapping("/servers/delete")
    @ApiOperation("删除实例")
    public ResultUtil<?> deleteServers(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        ActionResponse response = novaService.deleteServer(tokenId, id);
        log.info(uri + ",删除实例" + LINE, "成功");
        return ResultUtil.success(response);
    }

    /**
     * @Author 李孟帅
     * @Description 获取实例VNC控制台
     * @Date 2020/7/24
     **/
    @GetMapping("/servers/console/get")
    @ApiOperation("获取实例VNC控制台")
    public ResultUtil<?> getConsole(HttpServletRequest request, @RequestParam("id") String id) throws NullTokenException {
        String tokenId = request.getHeader("tokenId");
        String uri = request.getRequestURI();
        VNCConsole console = novaService.getConsole(tokenId, id);
        log.info(uri + ",获取实例VNC控制台" + LINE, "成功");
        return ResultUtil.success(console);
    }


}
